package cn.kpi.service.attendance.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.attendance.AttTbDao;
import cn.kpi.dao.common.OrgSummaryDao;
import cn.kpi.entity.attendance.AttDetailEntity;
import cn.kpi.entity.attendance.AttSummaryEntity;
import cn.kpi.entity.attendance.AttTbBatchEntity;
import cn.kpi.service.attendance.IAttTbService;

@Service
public class AttTbServiceImpl implements IAttTbService {

    @Autowired
    private AttTbDao attTbDao;

    @Autowired
    private OrgSummaryDao orgSummaryDao;

    public List<AttSummaryEntity> GetAttTbListByWhere(String where) throws Exception {
        try {
            return attTbDao.GetAttTbListByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }


    public List<AttSummaryEntity> GetAttTbListByUser(String where, String orgid) throws Exception {
        try {
            return attTbDao.GetAttTbListByUser(where, orgid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }


    public List<AttSummaryEntity> GetAttTbListNotUser(String where, String orgid) throws Exception {
        try {
            return attTbDao.GetAttTbListNotUser(where, orgid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int UpdateApproverUser(Long approverUser, Long orgsummaryid) throws Exception {
        try {
            return attTbDao.UpdateApproverUser(approverUser,orgsummaryid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public int GetAttTbListCountByWhere(String where) throws Exception {
        try {
            return attTbDao.GetAttTbListCountByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int InsertAttTb(List<AttDetailEntity> lst) throws Exception {
        try {
            int result = 0;
            float cqts = 0;//出勤天数
            float jbts = 0;//加班天数
            float xjts = 0;//休假天数
            float tqjts = 0;//探亲假
            float hjts = 0;//婚假天数
            float bsjts = 0;//病事假天数
            float bjts = 0;//病假天数
            float sjts = 0;//事假天数
            float gsycjts = 0;//工伤、孕产假天数
            float gsts = 0;//工伤天数
            float syjts = 0;//孕产假天数
            float sangjts = 0;//丧假天数
            float qtts = 0;//其它天数
            int cdcc = 0;//迟到次数
            int ztcc = 0;//早退次数
            int kgcs = 0;//旷工次数
            float wcqtsxj = 0;//未出勤天数小计
            int mouth = lst.get(0).getMouth();
            Long userid = lst.get(0).getUserid();
            Long byuserid = lst.get(0).getByuserid();
            int year = lst.get(0).getYear();
            for (int i = 0; i < lst.size(); i++) {
                AttDetailEntity item = lst.get(i);
                cqts = cqts + item.getSfcq();
                jbts = jbts + item.getSfjb();
                xjts = xjts + item.getNxj();//休假天数

                bsjts = bsjts + item.getBj() + item.getSj();
                bjts = bjts + item.getBj();//病假天数
                sjts = sjts + item.getSj();//事假天数

                gsycjts = gsycjts + item.getYgzs() + item.getSyj();
                gsts = gsts + item.getYgzs();//工伤天数
                syjts = syjts + item.getSyj();//孕产假天数
                cdcc = cdcc + item.getCd();
                ztcc = ztcc + item.getZt();
                kgcs = kgcs + item.getKg();

                hjts += item.getHj();//婚假天数
                tqjts += item.getTqj();//探亲假
                sangjts += item.getSj();//丧假天数
                qtts += item.getQt();//其它天数
                wcqtsxj += item.getNxj() + item.getHj() + item.getTqj() + item.getSyj() + item.getSj() + item.getYgzs() + item.getBj() + item.getSj() + item.getQt();
            }
            int sfqq = 1;
            if (cqts != lst.size()) {
                sfqq = 0;
            }


            AttSummaryEntity sumentity = new AttSummaryEntity();
            sumentity.setBjts(bjts);
            sumentity.setBsjts(bsjts);
            sumentity.setByuserid(byuserid);
            sumentity.setCdcc(cdcc);
            sumentity.setCqts(cqts);
            sumentity.setGsts(gsts);
            sumentity.setGsycjts(gsycjts);
            sumentity.setJbts(jbts);
            sumentity.setKgcs(kgcs);
            sumentity.setMouth(mouth);
            sumentity.setSjts(sjts);
            sumentity.setStatus(0);
            sumentity.setSyjts(syjts);
            sumentity.setUserid(userid);
            sumentity.setXjts(xjts);
            sumentity.setYear(year);
            sumentity.setZtcc(ztcc);

            sumentity.setSfqq(sfqq);
            sumentity.setTqjts(tqjts);
            sumentity.setHjts(hjts);
            sumentity.setSangjts(sangjts);
            sumentity.setQtts(qtts);
            sumentity.setWcqtsxj(wcqtsxj);

            result += attTbDao.InsertAttSummary(sumentity);
//			for(int i=0;i<lst.size();i++){
//				lst.get(i).setAttsummaryid(sumentity.getAttsummaryid());
//			}
            result += attTbDao.InsertAttDetailBatch(lst, sumentity.getAttsummaryid());
            if (result <= 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return -1;
            }
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    public AttSummaryEntity GetAttTbSummaryByWhereForMzpd(String where) throws Exception {
        try {
            return attTbDao.GetAttTbSummaryByWhereForMzpd(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<AttSummaryEntity> GetAttTbSummaryListByWhereForMzpd(String where) throws Exception {
        try {
            return attTbDao.GetAttTbSummaryListByWhereForMzpd(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Transactional
    public int CreateInsertAttTb(AttTbBatchEntity entity) throws Exception {
        try {
            int result = 0;
            entity.setOrgsmytype("att");
            result += orgSummaryDao.InsertOrgSummary(entity);
            Long orgsummaryid = entity.getOrgsummaryid();
            //System.out.println(orgsummaryid);
            entity.setOrgsummaryid(orgsummaryid);

            result += attTbDao.InsertAttSummaryByOrgId(entity);
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < entity.getDays(); i++) {
                list.add(i + 1);
            }
            result += attTbDao.InsertAttDetailBatchByDays(list, entity);
            result += attTbDao.UpdateSuyIdBatch(entity);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int CreateInsertAttTbNoDetail(AttTbBatchEntity entity) throws Exception {
        try {
            int result = 0;
            entity.setOrgsmytype("att");
            result += orgSummaryDao.InsertOrgSummary(entity);
            Long orgsummaryid = entity.getOrgsummaryid();
            //System.out.println(orgsummaryid);
            entity.setOrgsummaryid(orgsummaryid);

            result += attTbDao.InsertAttSummaryByOrgId(entity);

            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Override
    public int CreateInsertAttTbByUserid(AttTbBatchEntity entity) throws Exception {
        try {
            int result = 0;
            result = attTbDao.InsertAttSummaryByUserId(entity);

            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public AttSummaryEntity GetAttTbAttsummaryById(Long attsummaryid) throws Exception {
        try {
            return attTbDao.GetAttTbAttsummaryById(attsummaryid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<AttSummaryEntity> GetAttTbListByWhereOrderBy(String where) throws Exception {
        try {
            return attTbDao.GetAttTbListByWhereOrderBy(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int UpdateAttKH(Long orgsummaryid, Long szdw, Long rsgxszdw, String szdwname, String rsgxszdwname, Long attsummaryid) {
        try {
            return attTbDao.UpdateAttKH(orgsummaryid,szdw,rsgxszdw,szdwname,rsgxszdwname,attsummaryid);
        } catch (Exception ex) {
            return 0;
        }


    }

    @Override
    public int DeleteKQ(Long attsummaryid) {
        return attTbDao.deleteKQ(attsummaryid);
    }

    @Override
    public List<AttTbBatchEntity> GetOrgSummaryList(Long orgid, String orgsmytype) throws Exception {
        try {
            return orgSummaryDao.GetOrgSummaryListByOrgid(orgid,orgsmytype);
        } catch (Exception ex) {
            return null;
        }

    }

    @Override
    public int ChangeOrgid(Long userid, Long orgid) {
        return 0;
    }
}
